package exchange;

import java.net.Socket;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Exchange {
	public static void main(String[] args) {
		Logger log = Logger.getLogger("log.log");
		log.log(Level.INFO, "Exchange is starting ...");

		// start socket Acceptor;
		BlockingQueue<Socket> socketQueue = new LinkedBlockingDeque<Socket>();
		int port = 2013;
		new Thread(new Acceptor(socketQueue, port)).start();
		log.log(Level.INFO, "Acceptor is started.");

		// start engines;
		String[] symbolsAllowed = { "AAPL", "IBM", "PTGL" };
		HashMap<String, ExchangeEngine> enginesMap = new HashMap<String, ExchangeEngine>();
		for (String symbol : symbolsAllowed) {
			ExchangeEngine engine = new ExchangeEngine(symbol);
			enginesMap.put(symbol, engine);
			new Thread(engine).start();
		}
		log.log(Level.INFO, " Engines are started.");

		// start newClientHeandler;
		String[] clientsAllowed = { "client1", "client2", "client3" };
		HashMap<String, ExchangeClient> clientMap = new HashMap<String, ExchangeClient>();
		new Thread(new NewConnectionHandler(socketQueue, clientsAllowed,
				clientMap, enginesMap)).start();
		log.log(Level.INFO, " NewConnectionHandler is started.");

		// start engine's message senders;
		for (String symbol : symbolsAllowed) {
			MessageSender engine = new MessageSender(enginesMap.get(symbol).outQueue, clientMap);
			new Thread(engine).start();			
		}
		log.log(Level.INFO, "message senders are started.");
		
	}
}
